package com.zzyl.service.impl;

import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.BedVo;
import net.minidev.json.writer.BeansMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    /**
     * 添加床位
     * @param bedDto
     */
    @Override
    public void addBed(BedDto bedDto) {

        Bed bed=new Bed();
        BeanUtils.copyProperties(bedDto,bed);
        bed.setCreateTime(LocalDateTime.now());
        bed.setUpdateTime(LocalDateTime.now());
        bed.setBedStatus(0);
        bed.setCreateBy(1L);
        bed.setUpdateBy(1L);
        try {
            bedMapper.addBed(bed);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.BED_INSERT_FAIL);
        }
    }

    /**
     * 修改床位
     * @param bedDto
     */
    @Override
    public void updateBed(BedDto bedDto) {
        Bed bed = new Bed();
        BeanUtils.copyProperties(bedDto,bed);
        bed.setUpdateBy(1l);
        bed.setUpdateTime(LocalDateTime.now());
        if(ObjectUtil.isNotEmpty(bedDto.getBedStatus())){
            bed.setBedStatus(bedDto.getBedStatus());
        }
        bedMapper.updateBed(bed);
    }

    /**
     * 根据id查询床位
     * @param id
     * @return
     */
    @Override
    public Bed readBedById(Long id) {
        Bed bed=bedMapper.getBedById(id);
        return bed;
    }

    /**
     * 删除床位
     * @param id
     */
    @Override
    public void deleteBedById(Long id) {
        bedMapper.deleteBedById(id);
    }

}

